package com.qs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.qs.ConStants;
import com.qs.annotation.Log;
import com.qs.commen.Response;
import com.qs.pojo.SysDept;
import com.qs.pojo.SysRole;
import com.qs.pojo.SysUser;
import com.qs.pojo.vo.UserVO;
import com.qss.service.DeptService;
import com.qss.service.RoleService;
import com.qss.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    private Logger loger= LoggerFactory.getLogger(this.getClass());

    @Reference
    public UserService userService;
    @Reference
    public DeptService deptService;
    @Reference
    public RoleService roleService;

    /**
     * 跳转到列表页面
     * @return
     */
    @GetMapping("/toData")
    public String toData(){
        return "user/data";
    }

    /**
     * 异步加载分页数据
     * @param user
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @PostMapping("/data")
    @ResponseBody
    @Log(moduleName = "用户管理模块",moduleType = "分页查询")
    public Response data(UserVO user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = ConStants.DEFAULTE_PAGE) Integer pageSize) throws JsonProcessingException {
       //创建日志对象 模块 用户 类型 分页查询用户当前登录的用户
        ObjectMapper objectMapper = new ObjectMapper();
        loger.debug(objectMapper.writeValueAsString(user));
        loger.info("query user,params"+objectMapper.writeValueAsString(user));

        PageInfo<SysUser> pageInfo = userService.queryUsers(user, pageNumber, pageSize);
        return new Response(200,pageInfo);
    }

    /**
     * 跳转到编辑或新增页面
     * @param user
     * @param model
     * @return
     */
    @GetMapping("/toEdit")
    public String toEdit(SysUser user, Model model){
        if(user!=null && user.getUserId()!=null){
            user = userService.queryUserById(user.getUserId());
            model.addAttribute("user",user);
        }
        SysDept dept=new SysDept();
        dept.setStatus(0);
        List<SysDept> depts = deptService.queryDepts(dept);
        model.addAttribute("depts",depts);
        SysRole role=new SysRole();
        role.setStatus(0);
        List<SysRole> roles = roleService.queryRole(role);
        model.addAttribute("roles",roles);
        return "user/edit";
    }

    /**
     * 新增或更新用户信息
     * @param user
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Response save(SysUser user){
        boolean status = userService.saveUser(user);
        if(status){
            return Response.success("保存成功");
        }
        return new Response(400,"操作失败");
    }
    @DeleteMapping("/deletes")
    @ResponseBody
    public Response delete(Integer[] ids){
        if (ids==null){
            loger.error("删除时删除的ids为空");
            return Response.error();
        }
        boolean status = userService.delUserById(ids);
        if(status) return Response.success("删除成功！");
        return Response.error();
    }

    @PostMapping("checkLoginName")
    @ResponseBody
    public  boolean checkLoginName(SysUser user){

        SysUser sysUser = userService.queryUserByUserName(user.getLoginName());
        if (sysUser==null)return true;
        return false;
    }
}
